var verify = sessionStorage.getItem('verify');
var account = sessionStorage.getItem('account');
var character = sessionStorage.getItem('character');

if(verify == 'true'){
    var name = sessionStorage.getItem('name');
}
else {
    alert('请登录！');
    location.href = "login.html";
}

//注销按钮事件
$('#signOut').click(function () {
    var verify = sessionStorage.setItem('verify', 'false');
    switch (verify) {
        case 'false':
            alert("请登录！");
            location.href = "logon.html";
    }
});

//modal隐藏，清空输入框
$('#modalAddSub').on('hidden.bs.modal', function () {
    document.getElementById('modalAddSub_textSubName').value = '';
});

//modal隐藏，清空输入框
$('#modalAddHome').on('hidden.bs.modal', function () {
    document.getElementById('modalJoinClass_textSubCode').value = '';
});



//学生，老师分别动态加载
switch (character) {
    case 'student':
        $('#joinClass').attr('class', 'btn btn-primary  btn-default');

        //动态加载学生的课程
        $.ajax({
            url: "/classtable?method=getAllStuSub",
            type: "get",
            data: {account: account},
            dataType: "json",
            success: function (result) {
                for (var i in result) {
                    var subCode = result[i].subCode;
                    var subName = result[i].subName;
                    var subStuNumber = result[i].subStuNumber;
                    var tName = result[i].tName;

                    $("#div_index").append("<div class='col-sm-6 col-md-3'>" +
                        "<a data-subName='" + subName + "' data-subCode='" + subCode + "' data-tName='" + tName + "' onclick='subInfo(this)' >" +
                        " <div class=\"thumbnail\">" +
                        "<h1 class=\"text-left\">" + subName + "</h1>" +
                        "<div class=\"caption\">" +
                        "<br>" +
                        "<h4>已加入人数：" + subStuNumber + "</h4>" +
                        "<br>" +
                        "<h4>" +
                        "<p class=\"left\">老师：" + tName + " <span style=\"float: right\">邀请码：" + subCode + "</span></p>" +
                        "</h4>" +
                        "</div>" +
                        "</div>" +
                        "</a>" +
                        "</div>"
                    );
                }
            }
        });
        break;
    case 'teacher':
        $('#addClass').attr('class', 'btn btn-primary btn-default');

        //动态加载老师的课程
        $.ajax({
            url: "/subject?method=getAllTeacherSub",
            type: "get",
            data: {account: account},
            dataType: "json",
            success: function (result) {
                for (var i in result) {
                    var subCode = result[i].subCode;
                    var subName = result[i].subName;
                    var subStuNumber = result[i].subStuNumber;

                    $("#div_index").append("<div class='col-sm-6 col-md-3'>" +
                        "<a data-subName='" + subName + "' data-subCode='" + subCode + "' onclick='subInfo(this)' >" +
                        "<div class='thumbnail'>" +
                        "<h1 class='text-left'>" + subName +
                        "</h1><div class='caption'>" +
                        "<br>" +
                        "<h4>已加入人数：" + subStuNumber +
                        "</h4>" +
                        "<br>" +
                        "<h4 style='float: right'>邀请码：" + subCode +
                        "</h4>" +
                        "<a class='btn btn-warning btn-sm' " +
                        "data-subCode='" + subCode +"'" +
                        "onclick='deleteSubject(this)'>删除课程</a>" +
                        "</div>" +
                        "</div>" +
                        "</a>" +
                        "</div>");
                }
            }
        });
        break;
}

//点按进入subInfo
function subInfo(a) {
    var subCode = $(a).data('subcode');
    var subName = $(a).data('subname');
    var tName = $(a).data('tname');
    sessionStorage.setItem("subCode", subCode);
    sessionStorage.setItem("subName", subName);
    sessionStorage.setItem("tName", tName);
    location.href = "subInfo.html";
}

//老师删除课程
function deleteSubject(btn){
    var subCode = $(btn).data('subcode');
    if (confirm('是否确认删除？')) {
        $.ajax({
            url: "/subject?method=delSub",
            type: "get",
            data: {subCode: subCode},
            dataType: "json",
            success: function (result) {
                if (result) {
                    alert("删除成功！");
                    location.reload();
                }
                else {
                    alert("删除失败");
                }
            }
        });
    }
}

//教师创建课程
$("#modalAddSub_btnSubmit").click(function () {
    var account = sessionStorage.getItem('account');
    var subName = $("#modalAddSub_textSubName").val();

    if (subName == "") {
        alert('课程名不能为空！');
        return false;
    }

    $.ajax({
        url: "/subject?method=addSub",
        type: "get",
        data: {subName: subName, account: account},
        dataType: "json",
        success: function (result) {
            if (result.verify) {
                var subCode = result.subCode;
                alert("添加成功!,课程代码是：" + subCode);
                location.reload();
            }
            else {
                alert("添加失败!");
                location.reload();
            }
        }
    })
});

//学生加入课程
$("#modalJoinClass_btnJoinSubject").click(function () {
    var account = sessionStorage.getItem('account');
    var subCode = $('#modalJoinClass_textSubCode').val();

    if (subCode == "") {
        alert('邀请码不能为空！');
        return false;
    }

    $.ajax({
        url: "/classtable?method=joinSub",
        type: "get",
        data: {subCode: subCode, account: account},
        dataType: "json",
        success: function (result) {
            if (result) {
                location.reload();
            }
            else {
                alert("加入失败!");
                location.reload();
            }

        }
    })
});




